import constant from './constant'
import {getDate} from './common'
let LocalDAO = {};
/**
 * 获取用户签到情况
 */
LocalDAO.getCheckInState = function (userCode) {
  return JSON.parse(localStorage.getItem(userCode));
};
/**
 * 存储用户抽奖情况
 * userCode: {
 *      ctnSign: '7',
 *      date: '20170910',
 *      hasDrawn: 'false'
 * }
 */
LocalDAO.setCheckInState = function (userCode, data) {
  localStorage.setItem(userCode, JSON.stringify(data));
};

LocalDAO.deleteCheckInState = function (userCode) {
  localStorage.removeItem(userCode);
};

LocalDAO.saveUserCode = function (userCode) {
  sessionStorage.setItem(constant.Storage.USER_CODE, userCode)
}

LocalDAO.getUserCode = function () {
  return sessionStorage.getItem(constant.Storage.USER_CODE)
}

LocalDAO.clearStorage = function () {
  localStorage.removeItem('undefined')
  for (let item in localStorage) {
    let str = localStorage[item]
    if (str.startsWith('{') && str.endsWith('}') && str.includes('date') && str.includes('signed')) {
      if (JSON.parse(str).date !== getDate()) localStorage.removeItem(item)
    }
  }
}

export default LocalDAO
